#!/bin/bash

set -e

tests=(
  default
  default_bytes
  backtrack
  backtrack_utf8bytes
  backtrack_bytes
  nfa
  nfa_utf8bytes
  nfa_bytes
  regex
  regex_inline
)
tmpdir=$(mktemp -d)
with_plugin=
coveralls_id=

while true; do
  case "$1" in
    --with-plugin)
      with_plugin=yes
      shift
      ;;
    --coveralls-id)
      coveralls_id="$2"
      shift 2
      ;;
    *)
      break
      ;;
  esac
done

if [ -n "$with_plugin" ]; then
  cargo test --manifest-path regex_macros/Cargo.toml --no-run --verbose
  kcov \
    --verify \
    --include-pattern '/regex/src/' \
    "$tmpdir/plugin" \
    $(ls -t ./regex_macros/target/debug/plugin-* | head -n1)
fi

cargo test --no-run --verbose
for t in ${tests[@]}; do
  kcov \
    --verify \
    --include-pattern '/regex/src/' \
    "$tmpdir/$t" \
    $(ls -t ./target/debug/"$t"-* | head -n1)
done

if [ -n "$coveralls_id" ]; then
  kcov --verify --coveralls-id=$coveralls_id --merge target/cov "$tmpdir"/*
else
  kcov --verify --merge target/cov "$tmpdir"/*
fi
